package com.goods.business.service.imp;

import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class HealthServiceImpl implements HealthService {
    @Resource
    private HealthMapper healthMapper;
    @Resource
    private UserService userService;
    @Override
    public boolean isReport() {
        Long userId = getUserId();
        List<Health> list = healthMapper.selectByUserId(userId);
        return !CollectionUtils.isEmpty(list);
    }



    @Override
    public Integer report(Health health) {
        Long userId = getUserId();
        health.setCreateTime(new Date());
        health.setUserId(userId);
        return healthMapper.insert(health);
    }

    @Override
    public List<Health> getHistory() {
        Long userId = getUserId();
        Health health = new Health();
        health.setUserId(userId);
        return healthMapper.select(health);
    }





//获取UserId
    private Long getUserId() {
        UserInfoVO info =null;
        try {
            info = userService.info();
        } catch (SystemException e) {
            e.printStackTrace();
        }
        String username = info.getUsername();
        User user = userService.findUserByName(username);
        return user.getId();
    }
}
